/*
 * TestDataModelBroker.java
 *
 * Created on January 21, 2007, 10:32 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package myApp.myDataModel;
/*
 * Copyright 2007 microneering, Inc and James Gamber
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 * 
 * BBDDemoApplication/myApp.myDataModel/TestDataModelBroker.java
 */
import java.sql.SQLException;
import java.util.logging.Logger;

import bbd.BBDAPI;
import bbd.BBDAPIPrincipal;
import bbd.BBDBroker;
import bbd.BBDConnection;
import bbd.BBDRowArrayList;

/**
 * Example of broker to select, insert, or update info for one group of APIs
 * TestInsert TestUpdate TestDelete
 * 
 * @param R 
 * @param L 
 * @author james gamber
 */
public class TestDataModelBroker<R extends TestDataModelRow, L extends BBDRowArrayList<R>> implements BBDBroker<R,L> {

	private static final Logger log = Logger
			.getLogger(TestDataModelBroker.class.getName());

	/**
	 * Keys used to find the API SQL
	 */
	private final BBDAPI<R> SELECT = new BBDAPI<R>("test", "TestSelect");

	private final BBDAPI<R> INSERT = new BBDAPI<R>("test", "TestInsert");

	private final BBDAPI <R>UPDATE = new BBDAPI<R>("test", "TestUpdate");

	private final BBDAPI<R> DELETE = new BBDAPI<R>("test", "TestDelete");

	static final BBDAPIPrincipal userAccess = new BBDAPIPrincipal("Duke", "Java");

	{
		INSERT.setBbdPrincipal(userAccess);
		SELECT.setBbdPrincipal(userAccess);
		UPDATE.setBbdPrincipal(userAccess);
		DELETE.setBbdPrincipal(userAccess);		
	}	
	
	public L select(final R row) {
		
		/** 
		 * Use this technique to keep a connection open.
		 * 
		 *  By generating a new principal just for this method, 
		 *  the BBDConnection will not reuse an existing connection for the same user.
		 *  
		 *  Since you will get a unique connection, that connection will not bve closed in the 
		 *  finalize method for BBDBeanConnection when another BBDConnection (with the same principal) is finalzied.
		 */
       SELECT.setRowClass((Class<R>)TestDataModelRow.class);

		final BBDConnection<R, L> myConnection = new BBDConnection<R, L>();
		L list = null;
		try {
			list = myConnection.executeQuery(SELECT, row.getName());
		} catch (final SQLException e) {
			log.severe(e.toString());
		}

		// convert list rows from BBDDefaultRows to TestDataModelRows

		return  list;
	}

	public int insert(final R row) {

		final BBDConnection<R, L> myConnection = new BBDConnection<R, L>();
		int rowsUpdated = -1;

		try {

			rowsUpdated = myConnection.executeUpdate(INSERT, row.getName(), row
					.getAddress(), row.getCity(), row.getState(), row.getZip());

		} catch (final SQLException e) {
			log.severe(e.toString());
		}

		return rowsUpdated;
	}

	public int insert(final L list) {

		int rowsInserted = 0;

		for (final R row : list) {
			rowsInserted = rowsInserted + insert(row);
		}
		return rowsInserted;
	}

	public int update(final R row) {

		final BBDConnection<R, L> myConnection = new BBDConnection<R, L>();
		int rowsUpdated = -1;

		try {

			rowsUpdated = myConnection.executeUpdate(UPDATE, row.getName(), row
					.getAddress(), row.getCity(), row.getState(), row.getZip());

		} catch (final SQLException e) {
			log.severe(e.toString());
		}

		return rowsUpdated;
	}

	public int update(final L list) {

		int rowsUpdated = 0;

		for (final R row : list) {
			rowsUpdated = rowsUpdated + update(row);
		}
		return rowsUpdated;
	}

	public int delete(final R row) {

		final BBDConnection<R, L> myConnection = new BBDConnection<R, L>();
		int rowDeleted = -1;

		try {

			rowDeleted = myConnection.executeUpdate(DELETE, row.getName());

		} catch (final SQLException e) {
			log.severe(e.toString());
		}

		return rowDeleted;
	}

	public int delete(final L list) {

		
		final BBDConnection<R, L> myConnection = new BBDConnection<R, L>();
		int rowDeleted = 0;

		try {
			for (final R row : list) {
				rowDeleted = rowDeleted
						+ myConnection.executeUpdate(DELETE, row.getName());
			}

		} catch (final SQLException e) {
			log.severe(e.toString());
		}

		return rowDeleted;
	}

	public void setPrincipal(String name, String password) {
		// TODO Auto-generated method stub
		
	}

}
